import sys, pygame, os, pornoganizer

size = width, height = 640, 480
black = 0, 0, 0
selected_color = 0x7e, 0xb0, 0xde, 255

class pyGamepornoganizer:
    
    def markImage(self):
        self.browser.toggleImageMark()
        
    def getNextImg(self):
        return self.browser.getNextImg()
    
    def getPreviousImg(self):
        return self.browser.getPreviousImg()
        
    def resize(self, image):
        rect = image.get_rect()
        relative_width = rect.width / (1.0 * (width))
        relative_height = rect.height / (1.0 * (height))
        #print relative_width, relative_height
        new_width = width
        new_height = height
        if relative_width > relative_height:
            # resize with
            new_height = rect.height/relative_width
        else:
            # resize height
            new_width = rect.width/relative_height
        #print new_width, new_height
        return pygame.transform.scale(image, (new_width, new_height))
    
    def center(self, rect):
        if rect.width < width:
            rect = rect.move((width-rect.width)/2, 0)
        if rect.height < height:
            rect = rect.move(0, (height-rect.height)/2)
        return rect
    
    def showImage(self, path):
        img = pygame.image.load(path)
        imgrect = img.get_rect()
        if imgrect.width > width or imgrect.height > height:
            img = self.resize(img)
            imgrect = img.get_rect()
        imgrect = self.center(imgrect)
        
        if self.browser.isMarked():
            self.screen.fill(selected_color)
            img.set_alpha(192)
            self.screen.blit(img, imgrect)
        else:
            self.screen.fill(black)
            self.screen.blit(img, imgrect)
        pygame.display.flip()
        self.browser.viewImage()
        return True
    
    def printMarked(self):
        self.browser.rewind()
        while self.browser.hasNextImg():
            img = self.browser.getNextImg()
            if self.browser.isMarked():
                print "+" + img
            elif self.browser.isViewed():
                print "-" + img

    def __init__(self, path):
        pygame.init()
        self.screen = pygame.display.set_mode(size)
        self.browser = pornoganizer.ImageBrowser(path)
    
    def __del__(self):
        pygame.display.quit()
    
    def run(self):
        if not self.browser.hasNextImg():
            return
        img = self.getNextImg()
        self.showImage(img)
        
        while 1:
            event = pygame.event.wait()
            if event.type == pygame.QUIT: 
                self.printMarked()
                return
        
            if event.type == pygame.KEYDOWN:
                if event.key == 32:
                    img = self.getNextImg()
                elif event.key == 8:
                    img = self.getPreviousImg()
                elif event.key == 109:
                    self.markImage()
                elif event.key == 27 or event.key == 113:
                    self.printMarked()
                    return
                if not self.showImage(img):
                    self.printMarked()
                    return

if __name__ == '__main__':
    #print sys.argv[1]
    tmp = pyGamepornoganizer(sys.argv[1])
    #tmp = pyGamepornoganizer('/home/girino/Desktop/old')
    tmp.run()
